package com.lyk.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.common.dao.BaseDao;
import com.common.entity.A_customer;
import com.common.entity.PageBean;
import com.lyk.dao.A_assetLvDao;
import com.lyk.dao.A_customerDao;

public class A_customerDaoImpl extends BaseDao<A_customer> implements A_customerDao {

	@Override
	public List<A_customer> findByCondition(Map<String, Object> mp) {
		List<Object> paramList=new ArrayList<Object>();
		StringBuffer sf=new StringBuffer();
		sf.append(" select * from a_customer where 1=1 ");
		if(mp!=null && mp.size()>0){
			if(mp.get("customer_name")!=null){
				sf.append("and customer_name like ?");
				paramList.add(mp.get("customer_name"));
			}
		}
		return findBySql(sf.toString(), paramList.toArray());
	}

	@Override
	public int delA_customer(Integer n) {
		String sql = "delete a_customer where customer_id = ?";
		return this.executeUpdate(sql, new Object[]{n});
	}
	
	@Override
	public A_customer findById(Integer n) {
		List<Object> paramList=new ArrayList<Object>();
		StringBuffer sf=new StringBuffer();
		sf.append(" select * from a_customer where customer_id = ? ");
		paramList.add(n);
		List<A_customer> list = findBySql(sf.toString(), paramList.toArray());
		return (list!=null&&list.size()>0)?list.get(0):null;
	}
	
	public A_customer getEntity(ResultSet rs){
		try {
			A_customer customer = new A_customer();
			customer.setCustomer_id(rs.getInt("customer_id"));
			customer.setCustomer_name(rs.getString("customer_name"));
			customer.setPwd(rs.getString("pwd"));
			customer.seteMail(rs.getString("eMail"));
			customer.setTelephone(rs.getString("telephone"));
			customer.setRealName(rs.getString("realName"));
			customer.setAddress(rs.getString("address"));
			customer.setCredit(rs.getInt("credit"));
			customer.setAssetLv_id(rs.getInt("assetLv_id"));
			customer.setAsset(rs.getLong("asset"));
			return customer;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public PageBean<A_customer> findPageByCondition(int pageIndex,
			int pageSize, Map<String, Object> mp) {
		PageBean<A_customer> pageBean=new PageBean<A_customer>();
		pageBean.setPageIndex(pageIndex);
		pageBean.setPageSize(pageSize);
		List<Object> paramList=new ArrayList<Object>();
		StringBuffer querySql=new StringBuffer();
		querySql.append(" select * from a_customer where 1=1");
		if(mp!=null && mp.size()>0){
			if(mp.get("customer_name")!=null){
				querySql.append("and customer_name like ?");
				paramList.add(mp.get("customer_name"));
			}
		}
		pageBean.setTotalCount(this.getCount(querySql.toString(), paramList.toArray()));
		if(pageBean.getTotalCount()>0){
			List<A_customer> list = findPage(pageBean, paramList, querySql);
			pageBean.setList(list);
		}
		
		return pageBean;
	}
	
	private List<A_customer> findPage(PageBean<A_customer> pageBean,
			List<Object> paramList, StringBuffer querySql) {
		List<A_customer> list=new ArrayList<A_customer>();
		StringBuffer queryPageSql=new StringBuffer();
		queryPageSql.append(" select * from (");
		queryPageSql.append("  select tmp.*,rownum rn from ( ");
		queryPageSql.append(querySql.toString());
		queryPageSql.append("  ) tmp ");
		queryPageSql.append(" ) where rn>? and rn<=? ");
		int startRow=(pageBean.getPageIndex()-1)*pageBean.getPageSize();
		int endRow=pageBean.getPageIndex()*pageBean.getPageSize();
		paramList.add(startRow);
		paramList.add(endRow);
		Connection conn=this.getConnection();
		PreparedStatement prs=null;
		ResultSet rs=null;
		try {
			conn=this.getConnection();
			prs=conn.prepareStatement(queryPageSql.toString());
			Object[] objs=paramList.toArray();
			if(objs!=null && objs.length>0){
				for(int i=0;i<objs.length;i++){
					prs.setObject(i+1, objs[i]);
				}
			}
			rs=prs.executeQuery();
			
			while(rs.next()){
				A_customer customer = new A_customer();
				A_assetLvDao assetLvDao = new A_assetLvDaoImpl();
				customer.setCustomer_id(rs.getInt("customer_id"));
				customer.setCustomer_name(rs.getString("customer_name"));
				customer.setPwd(rs.getString("pwd"));
				customer.seteMail(rs.getString("eMail"));
				customer.setTelephone(rs.getString("telephone"));
				customer.setRealName(rs.getString("realName"));
				customer.setAddress(rs.getString("address"));
				customer.setCredit(rs.getInt("credit"));
				customer.setAssetLv_id(rs.getInt("assetLv_id"));
				customer.setAsset(rs.getLong("asset"));
				customer.setAssetLv(assetLvDao.findById(rs.getInt("assetLv_id")));
				list.add(customer);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
}
